package services;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import models.BackInfo;
import models.mappers.BackInfoMapper;
import models.mappers.DeviceMapper;

import org.apache.ibatis.session.SqlSession;

import play.mvc.Finally;

import com.aton.db.SessionFactory;

import vos.BackInfoSearchVo;
import vos.BackInfoVo;
import vos.DeviceVo;

public class BackInfoService {
	private static final SimpleDateFormat monthFormater = new SimpleDateFormat("yyyy-MM");
	/**
	 * 统计返款信息总数
	 *
	 * @Title: countBackInfo 
	 * @param vo
	 * @return
	 * @return: long
	 */
	public static long countBackInfo(BackInfoSearchVo vo) {
		SqlSession ss = SessionFactory.getSqlSession();
		try {
			BackInfoMapper mapper = ss.getMapper(BackInfoMapper.class);
            return mapper.countBackInfo(vo);
        } finally {
            ss.close();
        }
	}
	/**
	 * 统计返款信息列表
	 *
	 * @Title: listBackInfo 
	 * @param vo
	 * @return
	 * @return: List<BackInfoVo>
	 */
	public static List<BackInfoVo> listBackInfo(BackInfoSearchVo vo) {
		SqlSession ss = SessionFactory.getSqlSession();
		try {
			BackInfoMapper mapper = ss.getMapper(BackInfoMapper.class);
            return mapper.listBackInfo(vo);
        } finally {
            ss.close();
        }
	}
	/**
	 * 批量返款
	 *
	 * @Title: betchBack 
	 * @param listid
	 * @param operatorid
	 * @return
	 * @return: boolean
	 */
	public static boolean betchBack(List<String> listid, String operatorid) {
		// TODO Auto-generated method stub
		SqlSession ss = SessionFactory.getSqlSessionWithoutAutoCommit();
		try{
			Map<String,Object> map=new HashMap<>();
			map.put("listId", listid);
			Date date=new Date();
			map.put("backtime", date);
			map.put("backname", operatorid);
			BackInfoMapper mapper = ss.getMapper(BackInfoMapper.class);
			mapper.betchBack(map);
			ss.commit();
		}catch(Exception e){
			ss.rollback();
			e.printStackTrace();
			return false;
		}finally{
			ss.close();
		}
		return true;	
	}
	/**
	 * 根据id集合获取返款记录
	 *
	 * @Title: getByIds 
	 * @param listid
	 * @return
	 * @return: List<BackInfoVo>
	 */
	public static List<BackInfoVo> getByIds(List<String> listid) {
		SqlSession ss = SessionFactory.getSqlSession();
		try {
			BackInfoMapper mapper = ss.getMapper(BackInfoMapper.class);
			Map<String,Object> map=new HashMap<String,Object>();
			map.put("listId", listid);
            return mapper.getByIds(map);
        } finally {
            ss.close();
        }
	}
	/**
	 * 根据id获取返款信息
	 *
	 * @Title: getById 
	 * @param id
	 * @return
	 * @return: BackInfoVo
	 */
	public static BackInfoVo getById(Long id) {
		SqlSession ss = SessionFactory.getSqlSession();
		try {
			BackInfoMapper mapper = ss.getMapper(BackInfoMapper.class);
            return mapper.getById(id);
        } finally {
            ss.close();
        }
		
	}
	/**
	 * 修改返款信息
	 *
	 * @Title: updateBackInfo 
	 * @param backinfo
	 * @param operatorid 
	 * @return
	 * @return: boolean
	 */
	public static boolean updateBackInfo(BackInfo backinfo, String operatorid) {
		// TODO Auto-generated method stub
		SqlSession ss = SessionFactory.getSqlSessionWithoutAutoCommit();
		try{
			Date date=new Date();
			backinfo.backtime=date;
			backinfo.backname=operatorid;
			BackInfoMapper mapper = ss.getMapper(BackInfoMapper.class);
			mapper.updateBackInfo(backinfo);
			ss.commit();
		}catch(Exception e){
			e.printStackTrace();
			ss.rollback();
			return false;
		}finally{
			ss.close();
		}
		return true;
	}
	/**
	 * 批量驳回
	 *
	 * @Title: betchReject 
	 * @param listid
	 * @param operatorid
	 * @param rejreason 
	 * @return
	 * @return: boolean
	 */
	public static boolean betchReject(List<String> listid, String operatorid, String rejreason) {
		SqlSession ss = SessionFactory.getSqlSessionWithoutAutoCommit();
		try{
			Map<String,Object> map=new HashMap<>();
			map.put("listId", listid);
			Date date=new Date();
			map.put("modifytime", date);
			map.put("modifyname", operatorid);
			map.put("rejreason", rejreason);
			BackInfoMapper mapper = ss.getMapper(BackInfoMapper.class);
			mapper.betchReject(map);
			ss.commit();
		}catch(Exception e){
			ss.rollback();
			e.printStackTrace();
			return false;
		}finally{
			ss.close();
		}
		return true;
	}
	/**
	 * 驳回
	 *
	 * @Title: reject 
	 * @param id 记录id
	 * @param operatorid 操作人
	 * @param rejreason 驳回原因
	 * @return
	 * @return: boolean
	 */
	public static boolean reject(Long id, String operatorid, String rejreason) {
		SqlSession ss = SessionFactory.getSqlSessionWithoutAutoCommit();
		try{
			Map<String,Object> map=new HashMap<>();
			map.put("id", id);
			Date date=new Date();
			map.put("modifytime", date);
			map.put("modifyname", operatorid);
			map.put("rejreason", rejreason);
			BackInfoMapper mapper = ss.getMapper(BackInfoMapper.class);
			mapper.reject(map);
			ss.commit();
		}catch(Exception e){
			ss.rollback();
			e.printStackTrace();
			return false;
		}finally{
			ss.close();
		}
		return true;
	}
	/**
	 * 导出excel
	 *
	 * @Title: exportXls 
	 * @param vo
	 * @return
	 * @return: List<BackInfoVo>
	 */
	public static List<BackInfoVo> exportXls(BackInfoSearchVo vo) {
		SqlSession ss = SessionFactory.getSqlSession();
		try {
			BackInfoMapper mapper = ss.getMapper(BackInfoMapper.class);
            return mapper.exportXls(vo);
        } finally {
            ss.close();
        }
	}
	/**
	 * 获取后一个月的数据
	 *
	 * @Title: getFData 
	 * @param shopid
	 * @param cmonth
	 * @return
	 * @return: BackInfoVo
	 */
	public static BackInfoVo getFData(String shopid, String cmonth) {
		SqlSession ss = SessionFactory.getSqlSession();
		try {
			Map<String,String> fbdate=BackCountService.getFBMonth(monthFormater.parse(cmonth));
			String bmonth=fbdate.get("bmonth");
			BackInfoMapper mapper = ss.getMapper(BackInfoMapper.class);
            return mapper.getFData(shopid,bmonth);
        }catch(Exception e){
        	e.printStackTrace();
        	return null;
        } finally {
            ss.close();
        }
	}
	
}
